//
//  NSDictionary+JsonPrintf.m
//  AFNetworking
//
//  Created by Fucheng on 2019/4/11.
//

#import "NSDictionary+JsonPrintf.h"
#import "WJCDefine.h"
@implementation NSDictionary (JsonPrintf)

-(NSString *)printfJson
{
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        WJCLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
//    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    //去掉字符串中的转义符号
    NSMutableString *formatString = [NSMutableString stringWithString:mutStr];
    NSString *character = nil;
    for (int i = 0; i < formatString.length; i ++) {
        character = [formatString substringWithRange:NSMakeRange(i, 1)];
        if ([character isEqualToString:@"\\"])
            [formatString deleteCharactersInRange:NSMakeRange(i, 1)];
    }
    
    return formatString;
    
}

-(NSString *)logParagraph
{
    NSMutableArray *arr = [NSMutableArray new];
    for (NSString *key in self) {
        NSString *str;
        if ([self[key] isKindOfClass:[NSDictionary class]]||[self[key] isKindOfClass:[NSArray class]]) {
            str = [self[key] printfJson];
        }else{
            str = self[key];
        }
        [arr addObject:[NSString stringWithFormat:@"🚧🚧%@:%@🚧🚧",key,str]];
    }
    return [NSString stringWithFormat:@"‼️‼️‼️\n\n\n\n%@\n\n\n\n‼️‼️‼️",[arr componentsJoinedByString:@"\n"]];
}
@end
